package bookpos.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import bookpos.entity.Account;
import bookpos.entity.Role;
import bookpos.service.AccountService;
import bookpos.service.RoleService;
import bookpos.util.core.action.BaseAction;
import bookpos.util.core.action.Json;
import bookpos.util.core.util.ExceptionUtil;

import com.opensymphony.xwork2.ModelDriven;
/**
 * 账户控制器 AccountAction
 * @author WZB
 *
 */
@Controller
@Action(value = "account", results = { @Result(name = "account", location = "/page/account/account.jsp") })
public class AccountAction extends BaseAction implements ModelDriven<Account> {
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(AccountAction.class);
	@Autowired
	private AccountService service;
	@Autowired
	private RoleService roleService;
	public Account vo = new Account();
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = service.getTotal();
		List<Account> list = service.findAll(page, rows, sort, order);		
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	public void add(){
			Json result = new Json();
			try {
				service.save(vo);
				result.setSuccess(true);
				result.setMsg("添加账户成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加账户失败");					
			}
			System.out.println(result);
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	public void edit() {
			Json result = new Json();
			try {
				service.update(vo);
				result.setSuccess(true);
				result.setMsg("修改账户成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加账户失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	public void remove() {
			Json result = new Json();
			try {
				service.delete(vo.getId());
				result.setSuccess(true);
				result.setMsg("删除账户成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除账户失败");
			}
			writeJson(result);
	}
	public Account getModel() {
		return vo;
	}	
	public void getRoleList(){
		 List<Role> list = roleService.findAll();		
		 writeJson(list);
	}
}
